package renguanyu.app.auth4fx.dao.user;

import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import renguanyu.app.auth4fx.util.DbUtil;
import renguanyu.app.auth4fx.util.Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDao {

    public static Integer getUserTotal(String name, String password) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("""
        select
        count(*) as total
        from t_user
        where deleted = 'N'
        """);
        if (!name.isBlank()) {
            sql.append("""
            and name like ?
            """);
            params.add(name);
        }
        if (!password.isBlank()) {
            sql.append("""
            and password = ?
            """);
            params.add(password);
        }
        Integer total = DbUtil.query(sql.toString(), new ScalarHandler<>(), params.toArray());
        return total;
    }

    public static List<Map<String, Object>> getPagedUsers(Integer limit, Integer offset, String name, String password) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("""
        select
        t_user.id as user_id
        ,t_user.name as user_name
        ,t_user.password as password
        ,group_concat(t_role.name) as role_names
        from t_user
        left join t_user_role on t_user_role.user_id = t_user.id and t_user_role.deleted = 'N'
        left join t_role on t_role.id = t_user_role.role_id and t_role.deleted = 'N'
        where t_user.deleted = 'N'
        """);
        if (!name.isBlank()) {
            sql.append("""
            and t_user.name like ?
            """);
            params.add(name);
        }
        if (!password.isBlank()) {
            sql.append("""
            and t_user.password like ?
            """);
            params.add(password);
        }
        sql.append("""
        group by t_user.id
        """);
        sql.append("""
        order by t_user.updated_at desc
        """);
        sql.append("""
        limit ? offset ?
        """);
        params.add(limit);
        params.add(offset);
        return DbUtil.query(sql.toString(), new MapListHandler(), params.toArray());
    }

    public static Integer saveUser(String name,String password){
        String sql = """
        INSERT INTO t_user
        (id, deleted, created_by, created_at, updated_by, updated_at, name, password)
        VALUES(?, ?, ?, ?, ?, ?, ?, ?)
        """;
        List<Object> params = new ArrayList<>();
        params.add(Util.uuid());
        params.add("N");
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.add(name);
        params.add(password);
        return DbUtil.update(sql, params.toArray());
    }

    public static Integer updateUser(String name,String password,String id){
        String sql = """
        UPDATE t_user
        SET updated_by=?, updated_at=?, name=?, password=?
        WHERE id=?
        """;
        List<Object> params = new ArrayList<>();
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.add(name);
        params.add(password);
        params.add(id);
        return DbUtil.update(sql, params.toArray());
    }

    public static Integer deleteUser(List<String> ids, String inSql) {
        String sql = "UPDATE t_user SET deleted=?, updated_by=?, updated_at=? WHERE id IN (" + inSql + ")";
        List<Object> params = new ArrayList<>();
        params.add("Y");
        params.add(Util.getLoginName());
        params.add(Util.getCurrentDateTimeString());
        params.addAll(ids);
        return DbUtil.update(sql, params.toArray());
    }

}
